#/bin/bash
RESOURCE="/home/tools"
VERSION="cmake-3.6.2"
DOWNLOADURL="https://cmake.org/files/v3.6/${VERSION}.tar.gz"
# 检测wget工具是否安装
rpm -q wget &>/dev/null
if [ $? -ne 0 ]; then
    yum install wget -y
fi
rpm -q gcc &>/dev/null
if [ $? -ne 0 ]; then
    yum install gcc -y
fi
rpm -q gcc-c++ &>/dev/null
if [ $? -ne 0 ]; then
    yum install gcc-c++ -y
fi
# 创建目标文件家
if [ ! -d "${RESOURCE}" ]; then
    mkdir -p ${RESOURCE}
fi

function buildCMake() {
    if [ ! -d "${RESOURCE}/${VERSION}" ]; then
        cd ${RESOURCE}
        tar -zxvf ${VERSION}.tar.gz
    fi
    cd "${RESOURCE}/${VERSION}"
    ./bootstrap
    gmake && gmake install
    if [ $? -eq 0 ]; then
        ln -sf /usr/local/bin/cmake /usr/bin/cmake3
        cmake3 --version
        echo "cmake安装成功"
    else
        echo "cmake安装失败"
        exit 1
    fi
}

if [ ! -f "${RESOURCE}/${VERSION}.tar.gz" ]; then
    cd ${RESOURCE}
    wget ${DOWNLOADURL}
    buildCMake
else
    buildCMake
fi
